package com.kakarote.hrm.service.impl;


import com.kakarote.core.common.enums.DataAuthEnum;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.hrm.common.EmployeeHolder;
import com.kakarote.hrm.constant.MenuIdConstant;
import com.kakarote.hrm.entity.BO.QueryEmployeeDataBo;
import com.kakarote.hrm.entity.PO.HrmEmployeeDataMobilizeRecord;
import com.kakarote.hrm.entity.VO.DeptVO;
import com.kakarote.hrm.entity.VO.EmployeeChangeRecordVo;
import com.kakarote.hrm.mapper.HrmDeptMapper;
import com.kakarote.hrm.mapper.HrmEmployeeDataMobilizeRecordMapper;
import com.kakarote.hrm.service.IHrmDeptService;
import com.kakarote.hrm.service.IHrmEmployeeDataMobilizeRecordService;
import com.kakarote.hrm.utils.EmployeeUtil;
import io.seata.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 人员异动记录 服务实现类
 * </p>
 *
 * @author guomenghao
 * @since 2021-04-14
 */
@Service
public class HrmEmployeeDataMobilizeRecordImpl extends BaseServiceImpl<HrmEmployeeDataMobilizeRecordMapper, HrmEmployeeDataMobilizeRecord> implements IHrmEmployeeDataMobilizeRecordService {

    @Autowired
    private IHrmDeptService hrmDeptService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private EmployeeUtil employeeUtil;

    @Autowired
    private HrmDeptMapper deptMapper;

    @Autowired
    private HrmEmployeeDataMobilizeRecordMapper employeeDataMobilizeRecordMapper;
    @Override
    public void addMobilizeRecord(HrmEmployeeDataMobilizeRecord employeeDataMobilizeRecord) {
        this.save(employeeDataMobilizeRecord);
    }

    @Override
    public BasePage<EmployeeChangeRecordVo> queryPageList(QueryEmployeeDataBo queryEmployeeDataBo) {
        if(StringUtils.isEmpty(queryEmployeeDataBo.getTotalDate()) || queryEmployeeDataBo.getDeptId() == null){
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        }
        DateTimeFormatter yearMonthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        YearMonth yearMonth = YearMonth.parse(queryEmployeeDataBo.getTotalDate(), yearMonthFormatter);
        queryEmployeeDataBo.setFirstDay(yearMonth.atDay(1));
        queryEmployeeDataBo.setLastDay(yearMonth.atDay(yearMonth.lengthOfMonth()));

        if (queryEmployeeDataBo.getDeptId() != null){
            //部门检索
            List<Long> deptIds = hrmDeptService.selectChildrenDeptIds(queryEmployeeDataBo.getDeptId());
            queryEmployeeDataBo.setChildrenDeptIdList(deptIds);
            queryEmployeeDataBo.setDeptId(null);
        }
        List<Integer> dataAuthTypeList = adminService.queryDataTypeList(UserUtil.getUserId(), MenuIdConstant.DEPT_MENU_ID).getData();
        if (EmployeeHolder.isHrmAdmin() || dataAuthTypeList.contains(DataAuthEnum.ALL.getValue())) {
        }else {
            if (!dataAuthTypeList.isEmpty()) {
                Set<Long> deptIds = employeeUtil.queryDataAuthDeptId(dataAuthTypeList, MenuIdConstant.DEPT_MENU_ID);
                deptIds.add(-1L);
                queryEmployeeDataBo.setAuthDeptIds(deptIds);
            }
        }
        BasePage<EmployeeChangeRecordVo> page = employeeDataMobilizeRecordMapper.queryPageList(queryEmployeeDataBo.parse(), queryEmployeeDataBo);
        List<EmployeeChangeRecordVo> list = page.getList();
        if (list != null && !list.isEmpty()) {
            List<DeptVO> deptList = deptMapper.selectAllDeptList();
            for (int i = 0; i < list.size(); i++) {
                EmployeeChangeRecordVo record = list.get(i);
                List<String> deptNameList = new ArrayList<>();
                Map<Long, DeptVO> group = deptList.stream().collect(Collectors.toMap(DeptVO::getDeptId, v -> v));
                getDeptPath(deptNameList,group,record.getDeptId());
                record.setDeptPath(String.join("/", deptNameList));
            }
        }
        return page;
    }

    private List<String> getDeptPath(List<String> deptNameList, Map<Long, DeptVO> group, Long deptId) {
        if (group.containsKey(deptId)) {
            getDeptPath(deptNameList, group, group.get(deptId).getParentId());
            deptNameList.add(group.get(deptId).getName());
        }
        return deptNameList;
    }
}
